﻿//  Symplectic Spark
//  Copyright 2010 Symplectic Ltd
//  Created by Martyn Whitwell (martyn@symplectic.co.uk)

//  This file is part of Spark.

//  Spark is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  Spark is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with Spark.  If not, see <http://www.gnu.org/licenses/>.

// $URL: https://symplectic-spark.googlecode.com/svn/trunk/website/App_Code/Template.cs $
// $LastChangedDate: 2010-03-02 15:38:13 +0000 (Tue, 02 Mar 2010) $
// $LastChangedRevision: 11 $
// $LastChangedBy: martyn@symplectic.co.uk $

using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Xml.Linq;


/// <summary>
/// Summary description for Template
/// </summary>
namespace Symplectic.Spark
{
    public class Template
    {
        public List<string> ComponentNames { get; private set; }
        public List<string> InternalFunctionNames { get; private set; }
        public List<string> RequestSpecNames { get; private set; }
        public ElementTypeTemplate ElementTypeTemplate { get; private set; }


        #region Markup
        private string _markup = null;
        public string Markup
        {
            get
            {
                return _markup;
            }
            private set
            {
                _markup = value;

                ComponentNames = PopulateList(Global.RegexComponent, _markup);
                InternalFunctionNames = PopulateList(Global.RegexInternalFunction, _markup);
                RequestSpecNames = PopulateList(Global.RegexRequestSpec, _markup);
            }
        }
        #endregion

        public Template(string markup)
        {
            Markup = markup;
            //Markup = dynamicTemplate.ToString();
        }

        public Template(ElementTypeTemplate elementTypeTemplate)
        {
            //Load HTML template
            ElementTypeTemplate = elementTypeTemplate;
            string fullFilename = Path.Combine(Global.TemplatePathAbsolute, elementTypeTemplate.Template);
            if (File.Exists(fullFilename))
                Markup = File.ReadAllText(fullFilename);
            else
                throw new Exception("File " + fullFilename + " does not exist - please create it!");
        }

        private List<string> PopulateList(Regex regex, string text)
        {
            MatchCollection matches = regex.Matches(text);
            List<string> list = new List<string>(matches.Count);
            foreach (Match match in matches)
            {
                if (match.Groups["name"].Success)
                {
                    string name = match.Groups["name"].Value;
                    if (!list.Contains(name))
                        list.Add(name);
                }
            }
            return list;
        }
    }
}